from django import forms
from .models import Shift, Employee
import pandas as pd

class ScheduleForm(forms.Form):
    start_date = forms.DateField(
        label='开始日期',
        widget=forms.DateInput(attrs={'type': 'date'}),
        error_messages={'required': '请选择排班开始日期'}
    )
    days = forms.IntegerField(
        label='排班天数',
        min_value=1,
        max_value=30,
        error_messages={
            'required': '请输入排班天数',
            'min_value': '排班天数不能小于1天',
            'max_value': '排班天数不能超过30天'
        }
    )

class EmployeeImportForm(forms.Form):
    file = forms.FileField(label='选择文件', help_text='支持CSV或Excel格式')
    
    def clean_file(self):
        file = self.cleaned_data['file']
        if not file.name.endswith(('.csv', '.xlsx', '.xls')):
            raise forms.ValidationError('仅支持CSV或Excel文件格式')
        return file
    
    def process_file(self):
        file = self.cleaned_data['file']
        if file.name.endswith('.csv'):
            df = pd.read_csv(file)
        else:
            df = pd.read_excel(file)
            
        employees = []
        for _, row in df.iterrows():
            employees.append(Employee(
                name=row['name'],
                position=row.get('position', '')
            ))
        
        Employee.objects.bulk_create(employees)